<template>
  <div>
    <!--    搜索表单-->
    <div>
      <el-input style="width: 240px" placeholder="请输入昵称" v-model="params.username"></el-input>
      <el-input style="width: 240px; margin-left: 5px" placeholder="请输入联系方式" v-model="params.phone"></el-input>
      <el-button style="margin-left: 5px" type="primary" @click="load()">
        <i class="el-icon-search"></i> 搜索
      </el-button>
      <el-button style="margin-left: 5px" type="danger" @click="reset">
        <i class="el-icon-refresh"></i>重置
      </el-button>
    </div>
    <!--增删改查-->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-edit-outline"></i></el-button>
      <el-button type="danger" @click="delBatch" class="mr-5">批量删除 <i class="el-icon-remove-outline"></i></el-button>
    </div>
    <!--表单内容-->
    <el-table :data="tableData">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column fixed
                       prop="id"
                       label="编号"
                       width="50">
      </el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="phone" label="联系方式"></el-table-column>
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column prop="updateTime" label="更新时间"></el-table-column>

      <!--操作-->
      <el-table-column
          fixed="right"
          label="操作"
          width="300">
        <template v-slot="scope">
          <el-button @click="handleEdit(scope.row)" type="success" size="small">编辑<i class="el-icon-edit"></i>
          </el-button>

          <el-popconfirm
              class="ml-5"
              title="您确定删除这行信息吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button slot="reference" type="danger" size="small">删除 <i class="el-icon-remove-outline"></i>
            </el-button>
          </el-popconfirm>

          <el-button slot="reference" style="margin-left: 5px" type="warning" size="small"
                     @click="handleChangePassword(scope.row)">修改密码<i class="el-icon-remove-outline"></i>
          </el-button>

        </template>
      </el-table-column>


    </el-table>

    <!--    分页-->
    <div style="text-align: center; ">
      <el-pagination
          background
          :current-page="params.pageNum"
          :page-size="params.pageSize"
          layout="prev, pager, next"
          @current-change="handleCurrentChange"
          :total="total">
      </el-pagination>
    </div>

    <!--    新增表单-->
    <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="50%">
      <el-form label-width="80px" style="" :model="form" :rules="rules" ref="rulerForm">

        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

    <!--    //修改-->
    <el-dialog title="用户信息" :visible.sync="updateTab" width="50%">
      <el-form label-width="80px" style="" :model="updateForm" :rules="rules" ref="rulerForm">

        <el-form-item label="用户名" prop="username">
          <el-input v-model="updateForm.username" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="电话" prop="phone">
          <el-input v-model="updateForm.phone" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancelUpdate">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>

    <!--    修改密码-->
    <el-dialog title="修改密码" :visible.sync="dialogFormVisible2" width="30%">
      <el-form label-width="80px" style="" :model="passForm" :rules="rules" ref="rulerForm">

        <el-form-item label="密码" prop="newPass">
          <el-input v-model="passForm.newPass" auto-complete="off" show-password></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel2">取 消</el-button>
        <el-button type="primary" @click="savePass">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import request from "@/utils/request";
import Cookies from "js-cookie";

export default {
  name: "AdminList",

  data() {


    const checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号不能为空'));
      }
      if (!/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(value)) {
        callback(new Error('请输入正确的手机号'));
      }
      callback()
    }


    return {
      admin: Cookies.get('admin') ? JSON.parse(Cookies.get('admin')) : {},
      tableData: [],
      total: 0,
      params: {
        pageNum: 1,
        pageSize: 10,
        vip: '',
        phone: '',
      },

      dialogFormVisible: false,
      dialogFormVisible2: false,
      updateTab: false,
      form: {},
      passForm: {},
      updateForm: {},

      //表单核验规则
      rules: {
        phone: [
          {required: true, validator: checkPhone, triangle: 'blur'}
        ],
        newPass: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          // {min: 8, max: 16, message: '长度在8到16之间', trigger: 'blur'}
        ]
      },

    }
  },

  created() {
    this.load();
  },


  methods: {

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)
      request.post("/admin/del/batch", ids).then(res => {
        if (res.code === "200") {
          this.$message.success("删除成功");
          this.load();
        } else {
          this.$message.error("批量删除失败");
        }
      })
    },

    savePass() {
      this.$refs['rulerForm'].validate((valid) => {
        if (valid) {
          request.put('/admin/password', this.passForm).then(res => {
            if (res.code === '200') {
              this.$notify.success('修改成功')
              if (this.passForm.id === this.admin.id) {
                Cookies.remove('admin')
                this.$router.push('/login')
              }
            } else {
              this.$notify.error('修改失败')
            }
            this.load()
            this.dialogFormVisible2 = false
          })
        }
      })
    },

    handleChangePassword(row) {
      this.dialogFormVisible2 = true;
      this.passForm = JSON.parse(JSON.stringify(row))
    },
    handleCancel2() {
      this.dialogFormVisible2 = false;
      this.load()
    },

    load() {
      request.get('/admin/page', {
        params: this.params
      }).then(res => {
        if (res.code === '200') {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      })
    },

    reset() {
      this.params = {
        pageNum: 1,
        pageSize: 10,
        vip: '',
        phone: '',
      }
      this.load()
    },

    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum
      this.load()
    },

    handleAdd() {
      this.dialogFormVisible = true;
      this.form = {};
    },

    handleCancel() {
      this.dialogFormVisible = false;
      this.load()
    },

    del(id) {
      request.delete('/admin/delete/' + id).then(res => {
        if (res.code === '200') {
          this.$message.success('删除成功')
          this.load()
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    handleEdit(row) {
      this.updateForm = row;
      this.updateTab = true;
    },


    save() {
      this.$refs['rulerForm'].validate((valid) => {
        if (valid) {
          request.post('/admin/save', this.form).then(res => {
            if (res.code === '200') {
              this.$message.success('保存成功')
              this.dialogFormVisible = false
              this.load()
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })

    },
    update() {
      this.$refs['rulerForm'].validate((valid) => {
        if (valid) {
          request.put('/admin/update', this.updateForm).then(res => {
            if (res.code === '200') {
              this.$message.success('保存成功')
              this.updateTab = false
              this.load()
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })

    },
    handleCancelUpdate() {
      this.updateTab = false
      this.load()
    },


  }
}
</script>

<style scoped>

</style>